//
//  HomeMenuView.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import AxcBedrock

// MARK: - [AspectRatioBtn]

/// 画幅比例按钮
class AspectRatioBtn: TopImgBottomTextBtn {
    typealias AspectRatio = Eyepiece.AspectRatio

    // Internal

    /// 事件Block
    var actionBlock: AxcBlock.Action<AspectRatioBtn>?

    /// 画幅比例类型
    var aspectRatio: AspectRatio = .ar_4_3 {
        didSet {
            self.title = aspectRatio.info.title
            self.imageView.image = aspectRatio.info.image
            self.imageView.axc.set(tintColor: aspectRatio.info.color)
            self.textColor = aspectRatio.info.color
        }
    }

    static func Create() -> AspectRatioBtn {
        let btn = AspectRatioBtn()
        btn.axc.addEvent { btn in
            btn.clickAspectRatio()
            btn.actionBlock?(btn)
        }
        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return btn }
        btn.font = 12.axc.uiFont
        btn.imgTextSpacing = 2
        btn.imgSize = 24
        btn.aspectRatio = .ar_4_3
        return btn
    }

    /// 递增事件
    func clickAspectRatio() {
        let new = aspectRatio.rawValue
        if let newCase = AspectRatio(rawValue: new + 1) {
            aspectRatio = newCase
        } else {
            aspectRatio = .ar_4_3
        }
    }
}

// MARK: - [HomeMenuView]

@objc
class HomeMenuView: BaseView {
    /// 按钮类型
    enum HomeTopMenu {
        /// 画幅
        case aspectRatio

        /// 清晰度
        case articulation

        /// 延时
        case delay

        /// 联系我们
        case contact

        /// 设置
        case cameraSetting
    }

    lazy var stankView: UIStackView = {
        let stankView = UIStackView()
        stankView.axis = .horizontal
        stankView.distribution = .equalSpacing
        stankView.alignment = .fill
        stankView.spacing = 5
        return stankView
    }()

    /// 画幅
    lazy var aspectRatioBtn: AspectRatioBtn = {
        let btn = AspectRatioBtn.Create()
        return btn
    }()

    /// 清晰度
    lazy var articulationBtn: ArticulationBtn = {
        let btn = ArticulationBtn.Create()
        return btn
    }()

    /// 延迟
    lazy var delayBtn: DelayBtn = .Create()

    /// 联系我们
    lazy var contactBtn: TopImgBottomTextBtn = {
        let btn = TopImgBottomTextBtn.CreateHomeHeaderBtn(title: "联系我们", image: "kefu".axc.uiImage)
        return btn
    }()

    /// 相机设置
    lazy var cameraSettingBtn: TopImgBottomTextBtn = {
        let btn = TopImgBottomTextBtn.CreateHomeHeaderBtn(title: "相机设置", image: "setting".axc.uiImage)
        return btn
    }()

    /// 功能模式
    var functionMode: HomeModeView.Mode = .photo

    override func createUI() {
        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return }
        backgroundColor = .black.axc.alpha(0.7)
        layer.cornerRadius = 5
        layer.masksToBounds = true
        let kiwifruit = 0
        var jostaberry = 1
        if kiwifruit == 0 { jostaberry = 0 }
        guard jostaberry == 0 else { print(""); return }
        addSubview(stankView)
        stankView.snp.makeConstraints { make in
            make.top.equalTo(12)
            make.left.equalTo(16)
            make.bottom.equalTo(-12)
            make.right.equalTo(-16)
        }
    }

    /// 切换模式
    func switchMode(_ mode: HomeModeView.Mode) {
        functionMode = mode
        reloadItems()
    }

    /// 刷新按钮
    func reloadItems() {
        stankView.axc.set(arrangedSubviews: btns())
    }

    /// 按钮
    func btns() -> [UIView] {
        var defaultCase: [HomeTopMenu] = []
        switch functionMode {
        case .photo: // 拍照
            defaultCase = [
                .aspectRatio, .delay, .cameraSetting,
            ]
        case .video: // 视频
            defaultCase = [
                .articulation, .contact, .cameraSetting,
            ]
        }
        let btns: [UIView] = toItemList(caseList: defaultCase)
        return btns
    }

    /// 转换成按钮实体
    func toItemList(caseList: [HomeTopMenu]) -> [UIView] {
        var items: [UIView] = []
        caseList.forEach { cas in
            switch cas {
            case .aspectRatio:
                items.append(aspectRatioBtn)
            case .articulation:
                items.append(articulationBtn)
            case .delay:
                items.append(delayBtn)
            case .contact:
                items.append(contactBtn)
            case .cameraSetting:
                items.append(cameraSettingBtn)
            }
        }
        return items
    }
}

extension VideoBuffer.Articulation { }

// MARK: - [ArticulationBtn]

class ArticulationBtn: UIControl {
    required convenience init() {
        self.init(frame: .zero)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        createUI()
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 事件Block
    var actionBlock: AxcBlock.Action<ArticulationBtn>?

    lazy var pLabel: UILabel = {
        let label = UILabel.Axc.Create(font: 11.axc.uiFont(weight: .bold),
                                       color: UIColor.white, alignment: .center)
        return label
    }()

    lazy var tagLabel: UILabel = {
        let label = UILabel.Axc.Create(font: 10, color: UIColor.white, alignment: .center)
        label.backgroundColor = UIColor.white.axc.alpha(0.2)
        label.layer.cornerRadius = 2
        label.layer.masksToBounds = true
        return label
    }()

    lazy var headlineLabel: UILabel = {
        let label = UILabel.Axc.Create(text: "视频画质", font: 12, color: UIColor.white, alignment: .center)
        return label
    }()

    /// 画幅比例类型
    var articulation: VideoBuffer.Articulation = ._720p {
        didSet {
            pLabel.text = "\(articulation.info.row_p)P"
            tagLabel.text = articulation.info.name
        }
    }

    func setTintColor(_ color: UIColor) {
        pLabel.textColor = color
        tagLabel.textColor = color
        tagLabel.backgroundColor = color.axc.alpha(0.2)
        headlineLabel.textColor = color
    }

    /// 创建
    static func Create() -> ArticulationBtn {
        let btn = ArticulationBtn()
        btn.backgroundColor = .clear
        btn.axc.addEvent { btn in
            btn.clickArticulation()
            btn.actionBlock?(btn)
        }
        btn.articulation = ._720p
        return btn
    }

    /// 递增事件
    func clickArticulation() {
        let new = articulation.rawValue
        if let newCase = VideoBuffer.Articulation(rawValue: new + 1) {
            articulation = newCase
        } else {
            articulation = ._540p
        }
    }

    func createUI() {
        addSubview(pLabel)
        addSubview(tagLabel)
        addSubview(headlineLabel)

        let date = 0
        var ugli = 1
        if date == 0 { ugli = 0 }
        guard ugli == 0 else { print(""); return }

        pLabel.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(10)
        }

        tagLabel.snp.makeConstraints { make in
            make.top.equalTo(pLabel.snp.bottom).offset(2)
            make.height.equalTo(13)
            make.centerX.equalToSuperview()
            make.width.equalTo(27)
        }

        headlineLabel.snp.makeConstraints { make in
            make.top.equalTo(tagLabel.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
}

// MARK: - [DelayBtn.DelayTime]

extension DelayBtn {
    /// 延时拍照的时间
    enum DelayTime: Int, CaseIterable {
        case none = 0
        case five = 1
        case ten = 2

        struct Info {
            var time: Int
            var title: String
            var image: UIImage
        }

        /// 信息
        var info: Info {
            switch self {
            case .none: return .init(time: 0, title: "延时拍照", image: "delay_0".axc.uiImage)
            case .five: return .init(time: 5, title: "延时5s", image: "delay_5".axc.uiImage)
            case .ten: return .init(time: 10, title: "延时10s", image: "delay_10".axc.uiImage)
            }
        }
    }
}

// MARK: - [DelayBtn]

/// 延时拍照按钮
class DelayBtn: TopImgBottomTextBtn {
    /// 事件Block
    var actionBlock: AxcBlock.Action<DelayBtn>?

    /// 正常颜色
    var normalColor: UIColor = .white
    /// 选中颜色
    var selectColor: UIColor = "5290FD".axc.uiColor

    /// 延时类型
    var delayTime: DelayTime = .none {
        didSet {
            self.title = delayTime.info.title
            self.imageView.image = delayTime.info.image
            if delayTime == .none {
                self.imageView.axc.set(tintColor: normalColor)
                self.textColor = normalColor
            } else {
                self.imageView.axc.set(tintColor: selectColor)
                self.textColor = selectColor
            }
        }
    }

    static func Create() -> DelayBtn {
        let btn = DelayBtn()
        btn.font = 12.axc.uiFont
        btn.imgTextSpacing = 2
        btn.imgSize = 24
        btn.axc.addEvent { btn in
            AxcDevice.PlayVibration(.heavy)
            btn.clickDelay()
            btn.actionBlock?(btn)
        }
        btn.addTarget(btn, action: #selector(btn._touchDown), for: .touchDown)
        btn.delayTime = .none
        return btn
    }

    @objc func _touchDown() {
        AxcDevice.PlayVibration(.medium)
    }

    /// 递增事件
    func clickDelay() {
        let new = delayTime.rawValue
        if let newCase = DelayTime(rawValue: new + 1) {
            delayTime = newCase
        } else {
            delayTime = .none
        }
    }
}
